﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccess;

namespace XuaNhapKho
{
    public partial class NhanVien : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadGridView();
                loadData();
            }
        }

        private void loadData()
        {
            grvNhanvien.DataSource = NhanVienDAO.GetAll();
            grvNhanvien.DataBind();
        }

        private void loadGridView()
        {
            if (Request.QueryString["id"] != null)
            {
                int id = int.Parse(Request.QueryString["id"].ToString());
                DataTable dt = NhanVienDAO.GetBy(id).Tables[0];

                foreach (DataRow rows in dt.Rows)
                {
                    txtTenDangNhap.Text = rows["TenDangNhap"].ToString();
                    txtMatKhau.Text = rows["MatKhau"].ToString();
                    txtHoVaTen.Text = rows["HoVaTen"].ToString();
                    txtDiaChi.Text = rows["DiaChi"].ToString();
                    txtDienThoai.Text = rows["SoDienThoai"].ToString();
                    txtEmail.Text = rows["Email"].ToString();
                    ddlqQuyenHan.SelectedValue = rows["QuyenHan"].ToString();
                    if (rows["TinhTrang"].ToString() == "1")
                    {
                        rdoDiplayCenter.Checked = true;
                        rdoHiddenCenter.Checked = false;
                    }
                    else
                    {
                        rdoDiplayCenter.Checked = false;
                        rdoHiddenCenter.Checked = true;
                    }
                    
                }

            }
        }

        protected void lbtnEdit_Command(object sender, CommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            Response.Redirect("/NhanVien.aspx?id=" + id);
        }

        protected void lbtnRemove_Command(object sender, CommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            NhanVienDAO.Delete(id);
            Response.Redirect("/NhanVien.aspx");
        }

        protected void grvNhanvien_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvNhanvien.PageIndex = e.NewPageIndex;
            loadGridView();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string name = txtTenDangNhap.Text;
            string matkhau = txtMatKhau.Text;
            string hovaten = txtHoVaTen.Text;
            string email = txtEmail.Text;
            string sodienthoai = txtDienThoai.Text;
            string diachi=txtDiaChi.Text;
            string quyenhan = ddlqQuyenHan.SelectedValue; 
            int nhan = 0;
            if (rdoDiplayCenter.Checked==true) {
                nhan = 1;
            }
            if (rdoHiddenCenter.Checked==true) {
                nhan = 0;
            }

            if (Request.QueryString["id"] == null)
            {
                NhanVienDAO.Insert(name, matkhau, hovaten, email, sodienthoai, diachi, int.Parse(quyenhan),nhan);
                Response.Redirect("/NhanVien.aspx");
            }
            else
            {
                int id = int.Parse(Request.QueryString["id"].ToString());
                NhanVienDAO.Update(id, name, matkhau, hovaten, email, sodienthoai, diachi, int.Parse(quyenhan), nhan);
                Response.Redirect("/NhanVien.aspx");
            }
        }

    }
}